﻿using System;
using System.Collections.Generic;
using System.Text;
using LLFM.CAT.Model;
using System.Data.SqlClient;
using System.Data;
using Common;
using LLFM.CAT.Config;

namespace LLFM.CAT.Service
{
    public class UserService
    {
        public static List<M_UserInfo> GetUserListForPage(int status, int pageIndex, int pageSize, out int count, out int pageCount)
        {
            SqlParameter[] parms = {
                                       new SqlParameter("@Status",status)
                                       ,new SqlParameter("@PageIndex",pageIndex)
                                       ,new SqlParameter("@PageSize",pageSize)
                                     ,new SqlParameter("@RecordCount",SqlDbType.Int, 4, ParameterDirection.Output, false, 0, 0, string.Empty, DataRowVersion.Default, null),
                                     new SqlParameter("@PageCount",SqlDbType.Int, 4, ParameterDirection.Output, false, 0, 0, string.Empty, DataRowVersion.Default, null)
                                   };
            DataTable dt = SqlHelper.ExecuteDataset(DBConfig.BitautoSDT, CommandType.StoredProcedure, "pro_GetUserListForPage"
                , parms).Tables[0];
            count = Convert.ToInt32(parms[3].Value);
            pageCount = Convert.ToInt32(parms[4].Value);
            return ModelHelper.FillModelsFromDataTable<M_UserInfo>(dt);
        }

        public static M_User GetUserByIdAndIsAdmin(int id,bool IsAdmin)
        {
            DataTable dt = SqlHelper.ExecuteDataset(DBConfig.BitautoSDT
                , CommandType.StoredProcedure
                , "pro_GetUserByIdAndIsAdmin"
                , new SqlParameter("@Id", id)
                 , new SqlParameter("@IsAdmin", IsAdmin ? 1 : 0)).Tables[0];

            return ModelHelper.FillModel<M_User>(dt);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="passWord"></param>
        /// <param name="name"></param>
        /// <param name="IsAdmin"></param>
        /// <returns></returns>
        public static M_User GetUserByPassWordAndNameAndIsAdmin(string passWord,string name,bool IsAdmin,int status)
        {
            DataTable dt = SqlHelper.ExecuteDataset(DBConfig.BitautoSDT
                , CommandType.StoredProcedure
                , "pro_GetUserByPassWordAndNameAndIsAdmin"
                , new SqlParameter("@PassWord", passWord)
                , new SqlParameter("@Username", name)
                , new SqlParameter("@Status", status)
                 , new SqlParameter("@IsAdmin", IsAdmin ? 1 : 0)).Tables[0];
            return ModelHelper.FillModel<M_User>(dt);
        }

        /// <summary>
        /// 通过tel查找
        /// </summary>
        /// <param name="tel"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public static M_UserInfo GetUserInfoByTelAndStatus(string tel, int status)
        {
            DataTable dt = SqlHelper.ExecuteDataset(DBConfig.BitautoSDT
                , CommandType.StoredProcedure
                , "pro_GetUserInfoByTelAndStatus"
                , new SqlParameter("@Tel", tel)
                , new SqlParameter("@Status", status)).Tables[0];
            return ModelHelper.FillModel<M_UserInfo>(dt);
        }
    }
}
